﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Text.RegularExpressions;

namespace JustPlus
{
    /// <summary>
    /// SearchPanel.xaml 的交互逻辑
    /// </summary>
    public partial class SearchPanel : UserControl
    {
        private string context = "";
        public SearchPanel()
        {
            InitializeComponent();
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(mwnd.stackPanel2);
            mwnd.stackPanel1.Children.Add(mwnd.stackPanel3);
        }

        //开始搜索
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback((state) =>
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    Parameters parameters = new Parameters();
                    parameters.Add("search_text", textBox1.Text.Trim());
                    string url = ConnectionBase.ConstructUrlWithParameters("http://music.douban.com/subject_search", parameters);
                    string searchresult = new ConnectionBase().Get(url);
                    if (string.IsNullOrEmpty(searchresult))
                    {
                        textBlock1.Text = "搜索失败，请检查网络或者尝试重新搜索！";
                        return;
                    }
                    //搜索音乐人
                    if ((bool)radioButton1.IsChecked)
                    {
                        MatchCollection mc = Regex.Matches(searchresult, @"<div class=\""result-item musician\"".*?>.*?</h3>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                        if (mc.Count == 0)
                        {
                            textBlock1.Text = "没有搜索结果或者搜索到的音乐人非豆瓣音乐人，无法收听！";
                            return;
                        }
                        else
                        {
                            string temp = mc[0].Groups[0].Value;
                            string pictureTemp = Regex.Match(temp, @"<img.*?class=\""answer_pic\"".*?/?>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[0].Value;
                            string picture = Regex.Match(pictureTemp, @".*?src=\""([^\""]+)\""", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                            image1.Source = new BitmapImage(new Uri(picture, UriKind.Absolute));

                            string titleTemp = Regex.Match(temp, @"<a.*?class=\""nbg\"".*?/?>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[0].Value;
                            string title = Regex.Match(titleTemp, @".*?title=\""([^\""]+)\""", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                            textBlock1.Text = title;

                            string link = Regex.Match(titleTemp, @".*?href=\""([^\""]+)\""", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                            context = @"channel:0|musician_id:" + link.Substring(33, link.Length - 34);
                        }
                    }
                    else if ((bool)radioButton2.IsChecked)
                    {
                        MatchCollection mc = Regex.Matches(searchresult, @"<tr.*?class=\""item\"">.*?</tr>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                        if (mc.Count == 0)
                        {
                            textBlock1.Text = "没有搜索结果或者搜索到的专辑版权非豆瓣所有，无法收听！";
                            return;
                        }
                        string temp = mc[0].Groups[0].Value;
                        string pictureTemp = Regex.Match(temp, @"<img.*?/?>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[0].Value;
                        string picture = Regex.Match(pictureTemp, @".*?src=\""([^\""]+)\""", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                        image1.Source = new BitmapImage(new Uri(picture, UriKind.Absolute));

                        string titleTemp = Regex.Match(temp, @"<a.*?class=\""nbg\"".*?/?>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[0].Value;
                        string title = Regex.Match(titleTemp, @".*?title=\""([^\""]+)\""", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                        textBlock1.Text = title;

                        string link = Regex.Match(titleTemp, @".*?href=\""([^\""]+)\""", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                        context = @"channel:0|subject_id:" + link.Substring(32, link.Length - 33);
                    }
                    button2.Opacity = 0.6;
                }));
            }));
        }

        //播放搜索到的专辑/音乐人频道
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            GetSongList.currentchannel = new Channel(2, 0, @"搜索音乐");
            //if ((bool)defGeneBox1.Tag)
            {
                GenePanel.context = context;
            }

            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(mwnd.stackPanel2);

            PlayerPanel playerpanel = (PlayerPanel)mwnd.stackPanel3.Children[0];
            playerpanel.getsong.Next(true);
        }
    }
}
